package com.chainyoung.trademarket.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.custom_widget.EmptyCallback;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.trademarket.R;
import com.chainyoung.trademarket.adapter.InformationAdapter;
import com.chainyoung.trademarket.di.component.DaggerMainComponent;
import com.chainyoung.trademarket.di.component.DaggerMainFragmentComponent;
import com.chainyoung.trademarket.di.module.MainModule;
import com.chainyoung.trademarket.entity.Ad;
import com.chainyoung.trademarket.entity.Information;
import com.chainyoung.trademarket.mvp.contract.InformationContact;
import com.chainyoung.trademarket.mvp.presenter.InformationListPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.chainyoung.router.RouterConstants.PATH_INFORMATION;

//资讯
@Route(path = PATH_INFORMATION)
public class InfomationActivity extends BaseActivity<InformationListPresenter> implements InformationContact.View {

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    private InformationAdapter adapter;
    private List<Ad> mData = new ArrayList<>();
    private boolean isCanLoadMore;
    private int pageNo = 1;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent.builder()
                .appComponent(appComponent)
                .mainModule(new MainModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        setCurrentTitle("资讯");
        initRecyclerView();
    }

    private void initRecyclerView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new InformationAdapter(mData);
        rvList.setAdapter(adapter);
        DividerItemDecoration itemDecoration=new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(mContext,R.drawable.shape_divider_gray));
        rvList.addItemDecoration(itemDecoration);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isCanLoadMore) {
                    pageNo++;
                    getData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                getData();
            }
        });
    }

    @Override
    protected void initData() {
        pageNo = 1;
        getData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_infomation;
    }

    private void getData() {
        TreeMap<String,Object> map=new TreeMap<>();
        map.put("limit", LibConstant.PAGE_SIZ);
        map.put("p",pageNo);
        mPresenter.getInformationList(map);
    }




    @Override
    public void onGetInformationListSuccess(CommonList<Ad> data) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (pageNo==1){
            mData.clear();
        }
        if (data == null) {
            if (pageNo==1){
                showPageEmpty();
            }
            return;
        }
        if (data.getList()!=null){
            mData.addAll(data.getList());
            if (data.getList().size()<LibConstant.PAGE_SIZ){
                isCanLoadMore=true;
                refreshLayout.setEnableLoadMore(true);
            }else {
                isCanLoadMore=false;
                refreshLayout.setEnableLoadMore(false);
            }
        }

        if (mData.isEmpty()){
            showPageEmpty();
        }else {
            showPageContent();
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onGetInformationListFailed() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }
}
